package gotha;

import java.util.Comparator;

public class PlayerComparator implements Comparator<Player>,java.io.Serializable{
    public final static int NO_ORDER = 0;
    public final static int NUMBER_ORDER = 1;
    public final static int NAME_ORDER = 2;
    public final static int RANK_ORDER = 3;
    public final static int RATING_ORDER = 4;
    
    int playerOrderType = PlayerComparator.NO_ORDER;
    public PlayerComparator(int playerOrderType){
        this.playerOrderType = playerOrderType;
    }
    public int compare(Player p1, Player p2){
        switch (playerOrderType){
            case NAME_ORDER :
                int c = p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase());
                if (c != 0) return c;
                else return p1.getFirstName().toLowerCase().compareTo(p2.getFirstName().toLowerCase());
            case RANK_ORDER :
                if (p1.getRank() < p2.getRank()) return 1;
                if (p1.getRank() > p2.getRank()) return -1;
                c = p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase());
                if (c != 0) return c;
                else return p1.getFirstName().toLowerCase().compareTo(p2.getFirstName().toLowerCase());
            case RATING_ORDER :
                if (p1.getRating() < p2.getRating()) return 1;
                if (p1.getRating() > p2.getRating()) return -1;
                c = p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase());
                if (c != 0) return c;
                else return p1.getFirstName().toLowerCase().compareTo(p2.getFirstName().toLowerCase());
            default :
                return 0;
        }
    }
}

